package com.i2works.smartluolong.manage.service.admin.impl;


import com.i2works.smartluolong.manage.dal.admin.dao.IAdminOrgDAO;
import com.i2works.smartluolong.manage.dal.admin.dao.IAdminUserDAO;
import com.i2works.smartluolong.manage.entity.admin.*;
import com.i2works.smartluolong.manage.service.admin.IAdminUserService;
import com.i2works.smartluolong.manage.service.common.AdminPasswordHelper;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 管理员用户实现类
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminAccountServiceImpl
 * @date 2015年11月1日 下午4:36:53
 */
@Service("adminUserService")
public class AdminUserServiceImpl extends BaseCommonServiceImpl<AdminUserQuery, AdminUserDO> implements IAdminUserService {

    private final IAdminUserDAO adminUserDAO;

    private final AdminPasswordHelper adminPasswordHelper;

    @Autowired
    public AdminUserServiceImpl(AdminPasswordHelper adminPasswordHelper, IAdminUserDAO adminUserDAO, IAdminOrgDAO adminOrgDAO) {
        this.adminPasswordHelper = adminPasswordHelper;
        this.adminUserDAO = adminUserDAO;
    }

    /**
     * APP用户登录查询
     */
    @Override
    public Result queryAppLoginUser(AdminUserDO adminUserDO) throws ServiceException {
        try {
            //查询用户提交的账号信息
            AdminUserQuery accountQuery = new AdminUserQuery();
            accountQuery.setUserName(adminUserDO.getUserName());
            Integer userCount = adminUserDAO.queryCheck(accountQuery);
            if (userCount > 0) {
                AdminUserDO adminUser = adminUserDAO.query(accountQuery);
                String password = adminUser.getPassword();
                String encryptPassword = adminPasswordHelper.encryptPassword(adminUserDO.getPassword(),adminUser.getSalt());
                if (password.equalsIgnoreCase(encryptPassword)) {
                    AdminUserVO adminUserVO = new AdminUserVO();
                    BeanUtils.copyProperties(adminUser, adminUserVO);
                    return R.success(MsgEnum.ACCESS_LOGIN_SUCCESS.getMsg(), adminUserVO);
                } else {
                    return R.error(MsgEnum.ACCESS_ERROR.getMsg());
                }
            } else {
                return R.error(MsgEnum.ACCESS_UNKNOW.getMsg());
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void save(AdminUserDO adminUserDO) throws ServiceException {
        try {
            AdminUserRoleDO adminUserRoleDO = new AdminUserRoleDO();
            AdminUserOrgDO adminUserOrgDO = new AdminUserOrgDO();
            if (adminUserDO.getId() != null && adminUserDO.getId() != 0) {
                if (StringUtils.isNotBlank(adminUserDO.getPassword())) {
                    adminPasswordHelper.encryptPassword(adminUserDO);
                }
                adminUserDAO.update(adminUserDO);
                //更新权限
                adminUserRoleDO.setRoleId(adminUserDO.getRoleId());
                adminUserRoleDO.setUserId(adminUserDO.getId());
                //当前用户是否已存在权限，如果存在就更新，不存在就添加
                int roleExists = adminUserDAO.queryUserRoleExists(adminUserRoleDO);
                if (roleExists > 0) {
                    adminUserRoleDO.setStatus(GlobalConstant.status.TRUE);
                    adminUserRoleDO.setUpdateName(adminUserDO.getUpdateName());
                    adminUserDAO.updateUserRole(adminUserRoleDO);

                } else {
                    adminUserRoleDO.setUpdateName(adminUserDO.getUpdateName());
                    adminUserRoleDO.setCreateName(adminUserDO.getCreateName());
                    adminUserDAO.saveUserRole(adminUserRoleDO);
                }
                //更新组织机构
                adminUserOrgDO.setOrgId(adminUserDO.getOrgId());
                adminUserOrgDO.setUserId(adminUserDO.getId());
                //当前用户是否已存在组织架构，如果存在就更新，不存在就添加
                int orgExists = adminUserDAO.queryUserOrgExists(adminUserOrgDO);
                if (orgExists > 0) {
                    adminUserOrgDO.setStatus(GlobalConstant.status.TRUE);
                    adminUserOrgDO.setUpdateName(adminUserDO.getUpdateName());
                    adminUserDAO.updateUserOrg(adminUserOrgDO);

                } else {
                    adminUserOrgDO.setUpdateName(adminUserDO.getUpdateName());
                    adminUserOrgDO.setCreateName(adminUserDO.getCreateName());
                    adminUserDAO.saveUserOrg(adminUserOrgDO);
                }


            } else {
                //加密密码
                if (StringUtils.isBlank(adminUserDO.getPassword())) {
                    adminUserDO.setPassword(GlobalConstant.DEFAULT_PASSWORD);
                }
                adminPasswordHelper.encryptPassword(adminUserDO);
                adminUserDO.setCreateName(adminUserDO.getUpdateName());
                //返回成功条数，添加后的主键ID
                int count = adminUserDAO.save(adminUserDO);

                if (count > 0) {
                    //添加权限
                    adminUserRoleDO.setRoleId(adminUserDO.getRoleId());
                    adminUserRoleDO.setUserId(adminUserDO.getId());
                    adminUserDAO.saveUserRole(adminUserRoleDO);
                    //添加组织机构
                    adminUserOrgDO.setOrgId(adminUserDO.getOrgId());
                    adminUserOrgDO.setUserId(adminUserDO.getId());
                    adminUserDAO.saveUserOrg(adminUserOrgDO);

                }
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void updatePasswordChange(AdminUserDO adminUserDO) throws ServiceException {
        try {
            adminPasswordHelper.encryptPassword(adminUserDO);
            adminUserDAO.updatePasswordChange(adminUserDO);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

	@Override
	public String quryUserOrgCode(Integer userId) throws ServiceException {
		String orgCode=null;
		try {
		 orgCode=	adminUserDAO.quryUserOrgCode(userId);
		} catch (DAOException e) {
			 throw new ServiceException(e);
		}
		return orgCode;
	}

}
